/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package project.jpdftools.pdfmerger.actionlisteners;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import project.jpdftools.constants.Const.MOVE_ITEM;

/**
 *
 * @author Mido
 */
public class TableUpDownAction implements ActionListener {

    private JTable table;   // the table object to move the data in
    private int move_direction; // the moving direction

    /**
     * Constructor with parameters
     *
     * @param table
     * @param move_direction
     */
    public TableUpDownAction(JTable table, int move_direction) {
        super();
        this.table = table;
        this.move_direction = move_direction;
    }

    public void actionPerformed(ActionEvent e) {
        //This method can be called only when
        //there's a valid selection,
        //so go ahead and move the table rwo.
        int moveMe = table.getSelectedRow();

        // if no component is selected
        if (moveMe < 0) {
            return;
        }

        if (move_direction == MOVE_ITEM.move_up) {
            //UP ARROW BUTTON
            if (moveMe != 0) {
                //not already at top
                ((DefaultTableModel) table.getModel()).moveRow(moveMe, moveMe, moveMe - 1);
            }
        } else {
            //DOWN ARROW BUTTON
            if (moveMe != table.getRowCount() - 1) {
                //not already at bottom
                ((DefaultTableModel) table.getModel()).moveRow(moveMe, moveMe, moveMe + 1);
            }
        }
    }
}
